package cn.flysheep;

import java.io.Serializable;

/**
 * Created by FlySheep on 17/1/16.
 */
public class SimplePage implements Paginable, Serializable {
    private static final long serialVersionUID = 1322995425862308064L;

    public static final int DEFAULT_PAGE_SIZE = 20;

    protected int totalCount = 0;
    protected int pageSize = 20;
    protected int pageNO = 1;

    public static int currentPageNO(Integer pageNO) {
        return (pageNO == null) || (pageNO.intValue() < 1) ? 1 : pageNO.intValue();
    }

    public SimplePage() {
    }

    public SimplePage(int pageNO, int pageSize, int totalCount) {
        setTotalCount(totalCount);
        setPageSize(pageSize);
        setPageNO(pageNO);
        adjustPageNO();
    }

    public void setTotalCount(int totalCount) {
        if (totalCount < 0) {
            this.totalCount = 0;
        } else {
            this.totalCount = totalCount;
        }
    }

    public void setPageSize(int pageSize) {
        if (pageSize < 1) {
            this.pageSize = DEFAULT_PAGE_SIZE;
        } else {
            this.pageSize = pageSize;
        }
    }

    public void setPageNO(int pageNO) {
        if (pageNO < 1) {
            this.pageNO = 1;
        } else {
            this.pageNO = pageNO;
        }
    }

    private void adjustPageNO() {
        if (this.pageNO == 1) {
            return;
        }

        int totalPage = getTotalPage();

        if (this.pageNO > totalPage) {
            this.pageNO = totalPage;
        }
    }

    public int getTotalCount() {
        return this.totalCount;
    }

    public int getTotalPage() {
        int totalPage = this.totalCount / this.pageSize;
        if ((totalPage == 0) || (this.totalCount % this.pageSize != 0)) {
            totalPage++;
        }
        return totalPage;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public int getPageNO() {
        return this.pageNO;
    }

    public boolean isFirstPage() {
        return this.pageNO <= 1;
    }

    public boolean isLastPage() {
        return this.pageNO >= getTotalPage();
    }

    public int getNextPage() {
        if (isLastPage()) {
            return this.pageNO;
        }
        return this.pageNO + 1;
    }

    public int getPrePage() {
        if (isFirstPage()) {
            return this.pageNO;
        }
        return this.pageNO - 1;
    }
}
